//
//  KyDemoViewController.swift
//  KyTV
//
//  Created by fangyukui on 2017/10/26.
//  Copyright © 2017年 fangyukui. All rights reserved.
//

import UIKit

fileprivate let cellId = "cellId"

class KyDemoViewController: UIViewController {
    
    lazy var collectionView : UICollectionView =  {()->UICollectionView  in
        
        let layout =   KyWaterfallLayout()
        layout.itemSize = CGSize(width: 80, height: 80)
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.dataSource = self
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        let vc =  UICollectionView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.height), collectionViewLayout: layout)
        vc.delegate = self
        vc.dataSource = self
       return vc
    
    }()
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI();
        
        
        
        
        
        
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        navigationController?.setNavigationBarHidden(true, animated: true)
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    }
    

}

extension KyDemoViewController:KyEmitterable{
    fileprivate func setupUI(){
        view.backgroundColor = .black
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
        view.addSubview(collectionView)
        
    }
    fileprivate func setUpEmitter(){
        
    }
    
}


extension KyDemoViewController:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 80
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let item = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
        item.backgroundColor = UIColor.random
        return item
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let vc = UIViewController()
        vc.view.backgroundColor = .white
        
        
        navigationController?.pushViewController(vc, animated: true)
        
        setUpEmitter()
        
    }
    
}

extension KyDemoViewController:KyWaterfallLayoutDataSource{
    func numberOfColsInWaterfallLayout(layout: KyWaterfallLayout) -> Int {
         return 2
    }
    
    func waterfallLayout(layout: KyWaterfallLayout, hightForItem indexPath: IndexPath) -> CGFloat {
        return  CGFloat(arc4random_uniform(150) + 100)
    }
    
    
}


